/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package content;

import entity.*;
import java.util.*;
import javax.ejb.FinderException;
import javax.naming.*;
import transfer.User;
import transfer.UserRole;

/**
 *
 * @author Sergey
 */
public class UserContent {

    public static ArrayList getAllUsersNoAdmin() {
        try {
            Context context = new InitialContext();
            UsersLocalHome userhome = (UsersLocalHome) context.lookup("java:comp/env/Users");
            Collection collection = userhome.findAllUsersNoAdmin();
            Iterator iterator = collection.iterator();
            ArrayList array = new ArrayList();
            while (iterator.hasNext()) {
                array.add(((UsersLocal) iterator.next()).getUser());
            }
            return array;
        } catch (NamingException ex) {
            ex.printStackTrace();
        } catch (FinderException ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public static ArrayList getAllCustomers() {
        ArrayList customers = new ArrayList();
        try {
            Context context = new InitialContext();
            UsersLocalHome userhome = (UsersLocalHome) context.lookup("java:comp/env/Users");
            Collection collection = userhome.findAllCustomers();
            Iterator iterator = collection.iterator();
            while (iterator.hasNext()) {
                customers.add(((UsersLocal) iterator.next()).getUser());
            }
        } catch (NamingException ex) {
            ex.printStackTrace();
        } catch (FinderException ex) {
            ex.printStackTrace();
        }
        return customers;
    }

    public static User getUser(Integer id) {
        try {
            Context context = new InitialContext();
            UsersLocalHome usersHome = (UsersLocalHome) context.lookup("java:comp/env/Users");
            return ((UsersLocal) usersHome.findByPrimaryKey(id)).getUser();
        } catch (NamingException ex) {
            ex.printStackTrace();
        } catch (FinderException ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public static ArrayList getAllUserForRole(String role) {
        ArrayList customers = new ArrayList();
        try {
            Context context = new InitialContext();
            UsersLocalHome userhome = (UsersLocalHome) context.lookup("java:comp/env/Users");
            Collection collection = userhome.findByRole(role);
            Iterator iterator = collection.iterator();
            while (iterator.hasNext()) {
                customers.add(((UsersLocal) iterator.next()).getUser());
            }
        } catch (NamingException ex) {
            ex.printStackTrace();
        } catch (FinderException ex) {
            ex.printStackTrace();
        }
        return customers;
    }
    
    public static ArrayList getAllUsersForRole(UserRole role) {
        ArrayList users = new ArrayList();
        try {
            Context context = new InitialContext();
            UsersLocalHome userhome = (UsersLocalHome) context.lookup("java:comp/env/Users");
            Collection collection = userhome.findUsersForRole(role);
            Iterator iterator = collection.iterator();
            while (iterator.hasNext()) {
                users.add(((UsersLocal) iterator.next()).getUser());
            }
        } catch (NamingException ex) {
            ex.printStackTrace();
        } catch (FinderException ex) {
            ex.printStackTrace();
        }
        return users;
    }
}
